/**
  CNOK project, Anyang Normal University, IMP-CAS
  \class TAYaml
  \brief a singleton manager class to manage all kinds of parameters globally,
  typically reaction info, potential parameters, control parameters, and other
  miscellaneous ones.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2022/04/19
  \date Last modified: 2022/05/23 by SUN Yazhou
  \copyright 2020-2023 SUN Yazhou
  \copyright CNOK project, Anyang Normal University, IMP-CAS
*/

#ifndef TAParaManager_h
#define TAParaManager_h

#include <string>

using std::string;

#include "TAYaml.h"

class TAParaManager{
public:
  static TAParaManager *Instance();
  virtual ~TAParaManager();

  const TAYaml *GetPara() const{ return testnull(fConfig); }

  void Read(const string &file);
  template<typename T>
  void SetValue(const string &name, T v);
  void SetBaseDir(const string &dir);
  const char *BaseDir() const{ return fBaseDir.c_str(); }
  const char *BaseName() const{ return fBaseName.c_str(); }
  /// full name (including path) of the current yaml cfg file
  const char *cfg() const{ return fCfgYaml.c_str(); }
  void Clear(); ///< clean the previous text output of the base directory

protected:
  TAParaManager();
  /// check if p is null, and issue a warning if so
  const TAYaml *testnull(const TAYaml *p) const;

  static TAParaManager *fInstance;
  TAYaml *fConfig; ///< general configuration parameters
  string fBaseDir; ///< the directory to find the yaml files
  string fBaseName; ///< basename of the config file without ".yaml"
  string fCfgYaml; ///< path+yaml
};

template<typename T>
void TAParaManager::SetValue(const string &name, T v){
  testnull(fConfig);
  (*fConfig)[name] = v;
} // end member function SetValue

static TAParaManager *PARA = TAParaManager::Instance();

#endif
